package sv.com.seguros.web.controller.ctg;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import sv.com.seguros.dto.ctg.CtgAseguradora;
import sv.com.seguros.exception.WebException;
import sv.com.seguros.web.controller.BaseController;
import sv.com.seguros.web.util.DBContext;
import sv.com.seguros.web.util.WebUtilidades;

@Controller
@RequestMapping("/admin/aseguradoras")
public class CtgAseguradoraController extends BaseController{

	private static final long serialVersionUID = 6225391779182578991L;
	
	@RequestMapping(value = "view", method = {RequestMethod.POST})
	public String view(ModelMap model, 
			@RequestParam (required = false) String header) {
		model.put(WebUtilidades.HEADER_TITLE, StringUtils.isBlank(header) ? "ASEGURADORAS" : header);
		try {
			model.put("data", JSONArray.fromObject(DBContext.getCtgAseguradoraDao().findAllAsArray()));
		} catch (Exception e) {
			logger.error("view", e);
		}
		return "admin/catalogo/ctgAseguradora";
	}
	
	@RequestMapping(value = "save", method = {RequestMethod.POST})
	public void save(
			@ModelAttribute CtgAseguradora ctgAseguradora,
			HttpServletRequest request,
			HttpServletResponse response)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getBaseDao().save(ctgAseguradora);
			result.put("data", JSONArray.fromObject(DBContext.getCtgAseguradoraDao().findAllAsArray()));
			result.put("dataIndex", ctgAseguradora.getCtgAseguradoraId());
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		} catch (WebException e) {
			logger.error("save", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		} catch (Exception e) {
			logger.error("save", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		result.put("ctgAseguradoraId", ctgAseguradora.getCtgAseguradoraId());
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "delete", method = {RequestMethod.POST})
	public void delete(@RequestParam Long ctgAseguradoraId,
			HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCtgAseguradoraDao().delete(ctgAseguradoraId);
			result.put("data", JSONArray.fromObject(DBContext.getCtgAseguradoraDao().findAllAsArray()));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		} catch (WebException e) {
			logger.error("delete", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		} catch (Exception e) {
			logger.error("delete", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "toggle", method = {RequestMethod.POST})
	public void toggle(@RequestParam Long ctgAseguradoraId,
			@RequestParam String ctgAseguradoraActivo,
			HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCtgAseguradoraDao().toggleActive(ctgAseguradoraId, ctgAseguradoraActivo);
			result.put("data", JSONArray.fromObject(DBContext.getCtgAseguradoraDao().findAllAsArray()));
			result.put(SUCCESS, true);
			result.put(MESSAGE, UPDATED_DATA);
		} catch (WebException e) {
			logger.error("delete", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, e.getExceptionMessage());
		} catch (Exception e) {
			logger.error("delete", e);
			result.put(FAILURE, true);
			result.put(MESSAGE, DEFAULT_ERROR_MESSAGE);
		}
		serializeObject(result, response);
	}

}
